/***********************************************************************
 * Module:  MyRectPainter.java
 * Author:  Nikola
 * Purpose: Defines the Class MyRectPainter
 ***********************************************************************/

package genedit.view.graph;

import genedit.model.graph.GraphElement;
import genedit.model.graph.MyRect;
import genedit.utility.GraphUtil;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

public class MyRectPainter extends ElementPainter {
	/** @param element */
	public MyRectPainter(GraphElement element) {
		super(element);
		transform=new AffineTransform();
		shape = new java.awt.geom.Rectangle2D.Double(0, 0, element.getSize()
				.getWidth(), element.getSize().getHeight());
	}

	@Override
	public void paint(Graphics2D g) {
		MyRect element=(MyRect)getElement();
		
		Color backupColor=g.getColor();
		g.setColor(element.getColor());
		Stroke backupStroke = g.getStroke();
		g.setStroke(element.getStroke());
		
		Shape backupClip = g.getClip();
		
		AffineTransform backupTransformation=g.getTransform();
		refreshAffine();
		g.transform(transform);
		g.draw(shape);
		//
		Rectangle clip = new Rectangle((int)element.getSize().getWidth(),(int)element.getSize().getHeight());
		g.setPaint(Color.BLACK);
		g.setFont(new Font("Arial",Font.BOLD,9));
		g.clipRect(clip.x, clip.y, clip.width, clip.height);
		drawCenteredString(g,element.getName(),0,0,(int)element.getSize().getWidth(),(int)element.getSize().getHeight());
		//
		g.setTransform(backupTransformation);
		g.setStroke(backupStroke);
		g.setColor(backupColor);
		g.setClip(backupClip);
	}

	@Override
	public boolean isElementAt(Point2D p) {
		Point2D np=new Point2D.Double(p.getX(),p.getY());
		GraphUtil.inverseTransform(transform, np);
		return shape.contains(np);
	}

	@Override
	public void refreshAffine() {
		Point2D position=getElement().getPosition();
		transform=new AffineTransform();
		transform.translate(position.getX(), position.getY());
		transform.rotate(getElement().getRotation(), getElement().getSize().getWidth()/2, getElement().getSize().getHeight()/2);
	}
	
	@Override
	public void resizeShape() {
		shape = new java.awt.geom.Rectangle2D.Double(0, 0, element.getSize()
				.getWidth(), element.getSize().getHeight());
	}
	
	/**
	 * Metoda se koristi za iscrtavanje stringa u centru elementa
	 * 
	 * @param g2d kontekst u kojem se iscrtava string
	 * @param s string koji se iscrtava
	 * @param x početna X-koordinata MyRect
	 * @param y početna Y-koordinata MyRect
	 * @param width širina MyRect
	 * @param height visina MyRect
	 */
	public void drawCenteredString(Graphics2D g2d, String s, int x, int y, int width, int height) {
		// Nalazi veličinu stringa s odgovarajućeg fonta u konekstu g2d
	    FontMetrics fm   = g2d.getFontMetrics(g2d.getFont());
	    Rectangle2D rect = fm.getStringBounds(s, g2d);
	    int textHeight = (int)(rect.getHeight());
	    int textWidth  = (int)(rect.getWidth());
	    // Centrira tekst horizontalno i vertikalno za granice pravougaonika koje su mu prosleđene
	    int textX = x + (width - textWidth)/2;
	    int textY = y + (height - textHeight)/2 + fm.getAscent();
	    g2d.drawString(s, textX, textY);
	  }
}